/**
 * 页面验证工具类
 */

import { isUserAuthenticated } from './auth';

/**
 * 页面验证状态
 */
interface PageAuthState {
  showAuthModal: boolean;
  isAuthenticated: boolean;
}

/**
 * 页面验证混入对象
 * 在页面的setup函数中使用
 */
export const usePageAuth = () => {
  const { ref, onMounted } = require('vue');
  
  const showAuthModal = ref(false);
  const isAuthenticated = ref(false);

  // 检查验证状态
  const checkAuth = () => {
    if (isUserAuthenticated()) {
      isAuthenticated.value = true;
      showAuthModal.value = false;
    } else {
      isAuthenticated.value = false;
      showAuthModal.value = true;
    }
  };

  // 验证成功回调
  const handleAuthSuccess = () => {
    isAuthenticated.value = true;
    showAuthModal.value = false;
  };

  // 页面加载时检查验证
  onMounted(() => {
    checkAuth();
  });

  return {
    showAuthModal,
    isAuthenticated,
    checkAuth,
    handleAuthSuccess
  };
};
